package com.kiwilss.aopdemo.ui.paging

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.kiwilss.aopdemo.R

/**
 * @author : Administrator
 * e-mail : kiwilss@163.com
 * time   : 2021/02/02
 *  desc   :
 */
class PagingAdapter: PagingDataAdapter<PagingBean,PagingViewHolder>(diffUtil) {
    companion object{
        val diffUtil = object :DiffUtil.ItemCallback<PagingBean>(){
            override fun areItemsTheSame(oldItem: PagingBean, newItem: PagingBean): Boolean {
               return oldItem == newItem
            }

            override fun areContentsTheSame(oldItem: PagingBean, newItem: PagingBean): Boolean {
                return oldItem.name == newItem.name
            }

        }
    }

    override fun onBindViewHolder(holder: PagingViewHolder, position: Int) {
        holder.tvName.text = getItem(position)?.name
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PagingViewHolder {
        return PagingViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_paging,parent,false))
    }

}

class PagingViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
    val tvName: TextView = itemView.findViewById(R.id.tvItemPagingName)
}